/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * YearPlanRowWatcher.java
 * business
 * 2012-12-28上午10:44:05
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.report.rows;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.jackysoft.entity.Product;
import org.jackysoft.entity.YearPlan;
import org.jackysoft.service.ProductService;
import org.jackysoft.util.AbstractRowWatcher;
import org.jackysoft.util.ExcelUtil;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;


@Component
public class YearPlanRowWatcher extends AbstractRowWatcher {

	private ProductService pdtService;
	private List<String> errors = Lists.newArrayList();
	private Collection<YearPlan> yplan ;
	private int year;
	private boolean starup = false;
	public void starup(ProductService pdtService,Collection<YearPlan> yplan,List<String> errors,int year) {
		this.pdtService = pdtService;
		this.errors = errors;
		this.yplan = yplan;
		this.year = year;
		starup = true;
	}

	@Override
	public void accept(Row row) {
		
		if(!starup){
			errors.add("请先调用初始化操作starup");
			return;
		}
	    int rnum = row.getRowNum();
		Iterator<Cell> citr = row.cellIterator();
        String code  = ExcelUtil.nextCellString(citr);
        Double value = ExcelUtil.nextCellDouble(citr);
        Product pdt = pdtService.get(code);
	    if(pdt==null){
	    	errors.add("行:"+rnum+" 代码不规范,或者该产品不存在,代码:"+code);
	    	return;
	    }
	    YearPlan yp = new YearPlan();
	    yp.setHost(pdt);
	    yp.setValue(value);
	    yp.setYear(year);
	    yplan.add(yp);
	    
	}

}
